package com.franklin.ideaplugin.easytesting.ui.history;

import com.franklin.ideaplugin.api.ui.tree.BaseSimpleNode;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.util.NlsSafe;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiParameterList;
import com.intellij.psi.PsiType;
import com.intellij.ui.treeStructure.SimpleNode;

import javax.swing.*;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 方法节点
 *
 * @author Ye Junhui
 * @since 2023/6/21
 */
public class MethodNode extends BaseSimpleNode<HistoryNode> {

    private final PsiMethod interfaceClassName;

    protected MethodNode(SimpleNode aParent, PsiMethod interfaceClassName) {
        super(aParent);
        this.interfaceClassName = interfaceClassName;
    }

    @Override
    protected SimpleNode[] buildChildren() {
        return this.childrenNodeList.toArray(new SimpleNode[this.childrenNodeList.size()]);
    }

    @Override
    public @NlsSafe String getName() {
        PsiParameterList parameterList = interfaceClassName.getParameterList();
        PsiParameter[] parameters = parameterList.getParameters();
        String params = Arrays.stream(parameters)
                .map(PsiParameter::getType)
                .map(PsiType::getPresentableText)
                .collect(Collectors.joining(","));
        return interfaceClassName.getName() + String.format("(%s)", params);
    }

    @Override
    public Icon getMyIcon() {
        return AllIcons.Nodes.Method;
    }
}
